// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const db = cloud.database()
var $ = db.command.aggregate
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
  
  const wxContext = cloud.getWXContext()

  console.log(event.name)
  // 获取所有病人数据
  if(event.name == 'get_patients'){
    // var resault = await db.collection('patient').where({
    //   patient_del: false
    // }).limit(100).get()
    // return resault.data
    var resault_w;
    var resault = await db.collection('patient').aggregate()
    .lookup({
      from: 'user',
      localField: 'patient_chief',
      foreignField: '_openid',
      as: 'doctors_list',
    })
    .match({
      patient_del: false
    })
    .end()
    .then(res => {
      resault_w = res.list
    })
    .catch(err => 
      console.error(err))
    return resault_w
  }
  //根据id删除病人
  else if(event.name == 'del_patients'){
    var del_num = {}
    var upd_num = {}
    //删除病人
    del_num = await db.collection('patient').doc(event.id).update({
      data: ({
        patient_del: true
      })
    })
    //更新病房总体情况
    upd_num = await db.collection('ward').where({
      ward_submit: false
    }).update({
      data: {
        ward_out: _.push(event.patient_name)
      }
    })
    if(upd_num.stats.updated && del_num.stats.updated){
      return{
        stats: true
      }
    }
    else{
      return{
        stats: false
      }
    }
  }
  //获取所有术前成人信息
  else if(event.name == 'get_adults'){
    var resault = await db.collection('bo').where({
      bo_del: false,
      bo_age_type: '成人'
    }).limit(100).get()
    return resault.data
  }
  //根据id删术前成人
  else if(event.name == 'del_adults'){
    return await db.collection('bo').doc(event.id).update({
      data: ({
        bo_del: true
      })
    })
  }
  //获取所有术前儿童信息
  else if(event.name == 'get_children'){
    var resault = await db.collection('bo').where({
      bo_del: false,
      bo_age_type: '儿童'
    }).limit(100).get()
    return resault.data
  }
  //根据id删术前儿童
  else if(event.name == 'del_children'){
    return await db.collection('bo').doc(event.id).update({
      data: ({
        bo_del: true
      })
    })
  }
  //获取特殊交班 一次获取5个
  else if(event.name == 'get_shifts'){
    var data = []
    await db.collection('ss').aggregate().lookup({
      from: 'patient',
      localField: 'patient_id',
      foreignField: '_id',
      as: 'basic_info',
    })
    .replaceRoot({
      newRoot: $.mergeObjects([ $.arrayElemAt(['$basic_info', 0]), '$$ROOT' ])
    })
    .project({
      basic_info: 0
    })
    .match({
      patient_del: false
    })
    .skip(Number(event.count))
    .limit(5)
    .end()
    .then(res => {
      data = res.list
    })
    return data
  }
  //加载更多特殊交班 
  else if(event.name == 'more_shifts'){
    var data = []
    await db.collection('ss').aggregate().lookup({
      from: 'patient',
      localField: 'patient_id',
      foreignField: '_id',
      as: 'basic_info',
    })
    .replaceRoot({
      newRoot: $.mergeObjects([ $.arrayElemAt(['$basic_info', 0]), '$$ROOT' ])
    })
    .project({
      basic_info: 0
    })
    .match({
      patient_del: false
    })
    .skip(Number(event.count))
    .limit(1)
    .end()
    .then(res => {
      data = res.list
    })
    return data
  }
  //根据id删术前儿童
  else if(event.name == 'del_shifts'){
    return await db.collection('ss').doc(event.id).remove()
  }
  // 获取非本人的其他所有医生
  else if(event.name = 'get_doctors'){
    var doctors = []
    await db.collection('user').where({
      _openid: _.neq(wxContext.OPENID)
    }).limit(100).get()
    .then(res => {
      doctors = res.data
      for(var i=0;i<doctors.length;i++){
        doctors[i]['name'] = doctors[i]['user_name']
        doctors[i]['subname'] = doctors[i]['user_position']
        delete doctors[i]['user_name']
        delete doctors[i]['user_position']
      }
    })
    return doctors
  }
  

}